package com.shijiu.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shijiu.dao.mapper.CommentMapper;
import com.shijiu.pojo.Comment;
import com.shijiu.service.CommentService;
import com.shijiu.utils.UserThreadLocal;
import com.shijiu.vo.CommentListVo;
import com.shijiu.common.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author xlj
 * @Date Created in  2023/3/18 14:30
 * @DESCRIPTION:
 * @Version V1.0
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Resource
    private CommentMapper commentMapper;
    private static final String PAGE = "page";
    private static final String PAGE_SIZE = "pageSize";
    private static final String USER_NAME = "username";
    private static final String START_TIME = "startTime";
    private static final String END_TIME = "endTime";
    private static final String NAME = "name";
    private static final String STATUS = "status";
    @Override
    public Result queryCommentList(Map<String, Object> requestBodyMaps) {
        Integer pageCount = (Integer) requestBodyMaps.get(PAGE);
        Integer pageSize = (Integer) requestBodyMaps.get(PAGE_SIZE);
        String name = (String) requestBodyMaps.get(NAME);
        String status = (String) requestBodyMaps.get(STATUS);
        LambdaQueryWrapper<Comment> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //如果姓名不为 null 不为空，模糊查询
        if (StrUtil.isNotBlank(name)) {
            lambdaQueryWrapper.like(Comment::getName, "%" + name + "%");
        }
        //如果状态值不为空，查询状态值
        if (StrUtil.isNotBlank(status)) {
            lambdaQueryWrapper.eq(Comment::getStatus, status);
        }
        commentMapper.selectList(lambdaQueryWrapper);
        // 查询第pageCount页，每页pageSize条记录
        Page<Comment> page = new Page<>(pageCount, pageSize);
        IPage<Comment> commentPage = commentMapper.selectPage(page, lambdaQueryWrapper);
        // 当前页的记录列表
        List<Comment> commentList = commentPage.getRecords();
        // 总记录数
        long total = commentPage.getTotal();
        CommentListVo commentListVo = new CommentListVo();
        commentListVo.setCommentVoList(commentList);
        commentListVo.setTotal(total);
        return Result.success(commentListVo);
    }

    @Override
    public Result selectOrderListByPage(Integer shopId, Map<String, Object> requestBodyMaps)  {
        Integer pageCount = (Integer) requestBodyMaps.get(PAGE);
        Integer pageSize = (Integer) requestBodyMaps.get(PAGE_SIZE);
        String username = (String) requestBodyMaps.get(USER_NAME);
        String startTime = (String) requestBodyMaps.get(START_TIME);
        String endTime = (String) requestBodyMaps.get(END_TIME);
        LambdaQueryWrapper<Comment> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotBlank(username)) {
            lambdaQueryWrapper.like(Comment::getName, "%" + username + "%");
        }
        if (StrUtil.isNotBlank(startTime) && StrUtil.isNotBlank(endTime)) {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            LocalDateTime startDateTime = LocalDateTime.parse(startTime, formatter);
            LocalDateTime endDateTime = LocalDateTime.parse(endTime, formatter);
            Date startDate = Date.from(startDateTime.atZone(ZoneId.systemDefault()).toInstant());
            Date endDate = Date.from(endDateTime.atZone(ZoneId.systemDefault()).toInstant());
            lambdaQueryWrapper.between(Comment::getCreateDate, startDate, endDate);
        }

        lambdaQueryWrapper.eq(Comment::getShopId,
                        UserThreadLocal.get().get("shopId"))
                .orderByDesc(Comment::getCreateDate);
        commentMapper.selectList(lambdaQueryWrapper);
        // 查询第pageCount页，每页pageSize条记录
        Page<Comment> page = new Page<>(pageCount, pageSize);
        IPage<Comment> commentPage = commentMapper.selectPage(page, lambdaQueryWrapper);
        // 当前页的记录列表
        List<Comment> commentList = commentPage.getRecords();
        // 总记录数
        long total = commentPage.getTotal();
        CommentListVo commentListVo = new CommentListVo();
        commentListVo.setCommentVoList(commentList);
        commentListVo.setTotal(total);
        return Result.success(commentListVo);
    }

    @Override
    public Result deleteCommentById(Integer shopId ,Integer id) {
        if (shopId == null) {
            // 用户信息里没有 shopId，处理异常情况
            return Result.fail(Integer.parseInt("500"),"用户信息不完整");
        }
        LambdaQueryWrapper<Comment> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Comment::getShopId, shopId)
                .eq(Comment::getStatus, "2")
                .eq(Comment::getId, id);
        int deletedCount = commentMapper.delete(wrapper);
        if (deletedCount > 0) {
            return Result.success("删除成功");
        } else {
            return Result.fail(Integer.parseInt("500"),"删除失败");
        }
    }

}
